package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerPersonAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerRelationDepPerson;
import com.isunland.managesystem.entity.CustomerRelationDepPerson.DepartmentPerson;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

public class CustomerRelationListStaffFragment extends BaseListFragment {
  public static final String NEW = "new";
  //public static final String NEW_BACK = "newback";
  //public static final String PUBLISH = "publish";
  public static final String SUBMIT = "submit";
  protected static final String TAG = CustomerRelationStaffListFragment.class.getSimpleName();
  //public static final String EXTRA_DEPARTMENT = "com.isunland.managesystem.ui.extra_department";
  private static final int REQUEST_ADD = 1;
  private static final int REQUEST_ALTER = 0;
  private static String mBlongCustomerCode;
  private static String mBlongOrgCode;
  private CustomerPersonAdapter mAdapter;
  private boolean isDeptCreated;
  private ArrayList<DepartmentPerson> mList;

  @Override public void initData() {
    super.initData();
    mList = new ArrayList<>();
    mAdapter = new CustomerPersonAdapter(mActivity, mList);
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setListAdapter(mAdapter);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public String getUrl() {

    return ApiConst.URL_STAFF;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> deaprtMentPerson = new HashMap<>();
    deaprtMentPerson.put("curPage", "1");
    deaprtMentPerson.put("pageSize", "1000");
    if (!TextUtils.isEmpty(mBlongOrgCode)) {
      deaprtMentPerson.put("blongOrgCode", mBlongOrgCode);
    }
    if (!TextUtils.isEmpty(mBlongCustomerCode)) {
      deaprtMentPerson.put("blongCustomerCode", mBlongCustomerCode);
    }
    return deaprtMentPerson;
  }

  @Override public void resolveResponse(String json) {
    CustomerRelationDepPerson staff = new Gson().fromJson(json, CustomerRelationDepPerson.class);
    ArrayList<DepartmentPerson> departmentStaff = staff.getDataList();
    setAdapter(departmentStaff);
  }

  private void setAdapter(ArrayList<DepartmentPerson> departmentPerson) {
    if (departmentPerson == null) {
      departmentPerson = new ArrayList<>();
    }
    if (mAdapter == null) {
      mAdapter = new CustomerPersonAdapter(mActivity, departmentPerson);
      setListAdapter(mAdapter);
    }
    mList.clear();
    mList.addAll(departmentPerson);
    ((CustomerPersonAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_add:
        if (!TextUtils.isEmpty(mBlongOrgCode) && isDeptCreated) {
          Intent addPersonIntent =
              new Intent(getActivity(), CustomerRelationAddStaffActivity.class);
          addPersonIntent.putExtra(CustomerRelationAddStaffFragment.EXTRA_ORDCODE, mBlongOrgCode);
          addPersonIntent.putExtra(CustomerRelationAddStaffFragment.EXTRA_CUSTOMER_CODE,
              mBlongCustomerCode);
          startActivityForResult(addPersonIntent, REQUEST_ADD);
        } else {
          ToastUtil.show(R.string.firstSaveDepartment);
        }
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    DepartmentPerson departmentPerson = mAdapter.getItem(position - 1);
    Intent intent = new Intent(getActivity(), CustomerRelationStaffDetailActivity.class);
    intent.putExtra(CustomerRelationStaffDetailFragment.EXTRA_VALUE, departmentPerson);
    startActivityForResult(intent, REQUEST_ALTER);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    switch (requestCode) {
      case REQUEST_ADD:
        volleyPost();
        break;
      case REQUEST_ALTER:
        volleyPost();
        break;

      default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  // 刷新列表
  public void refresh() {
    //		volleyPost();
  }

  public void getData(String customerId, String orgId) {
    mBlongCustomerCode = customerId;
    mBlongOrgCode = orgId;
    LogUtil.i("mBlongCustomerCode===" + mBlongCustomerCode + ",mBlongOrgCode===" + mBlongOrgCode);
    isDeptCreated = true;
  }
}